//
//  WiteBoxStatisticsViewController.m
//  WiteBox
//
//  Created by John Blanco on 6/28/09.
//  Copyright 2009 Effective UI. All rights reserved.
//

#import "WiteBoxStatisticsViewController.h"

#import "TestLocationModel.h"
#import "TestCaseRun.h"
#import "UITestResultsBar.h"
#import "WiteBoxStyles.h"

// PRIVATE METHODS
@interface WiteBoxStatisticsViewController (Private)

- (NSString *)randomSuccessMessage;
- (NSString *)randomSomeFailureMessage;
- (NSString *)randomManyFailureMessage;
	
@end

@implementation WiteBoxStatisticsViewController

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	testResultsBar = [[UITestResultsBar alloc] initWithFrame:CGRectMake(68, 23, 184, 10)];
	
	passedTestsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 6, 68, 43)];
	passedTestsLabel.textColor = [WiteBoxStyles foregroundTextColor];
	passedTestsLabel.backgroundColor = [UIColor clearColor];
	passedTestsLabel.font = [UIFont boldSystemFontOfSize:28.0];
	passedTestsLabel.textAlignment = UITextAlignmentCenter;
	passedTestsLabel.adjustsFontSizeToFitWidth = YES;
	
	failedTestsLabel = [[UILabel alloc] initWithFrame:CGRectMake(252, 6, 68, 43)];
	failedTestsLabel.textColor = [WiteBoxStyles foregroundTextColor];
	failedTestsLabel.backgroundColor = [UIColor clearColor];
	failedTestsLabel.font = [UIFont boldSystemFontOfSize:28.0];
	failedTestsLabel.textAlignment = UITextAlignmentCenter;
	failedTestsLabel.adjustsFontSizeToFitWidth = YES;

	statusLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(21, 51, 280, 21)];
	statusLabel1.textColor = [WiteBoxStyles foregroundTextColor];
	statusLabel1.backgroundColor = [UIColor clearColor];
	statusLabel1.font = [UIFont boldSystemFontOfSize:16.0];
	statusLabel1.textAlignment = UITextAlignmentCenter;

	statusLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(21, 73, 280, 21)];
	statusLabel2.textColor = [WiteBoxStyles foregroundSubTextColor];
	statusLabel2.backgroundColor = [UIColor clearColor];
	statusLabel2.font = [UIFont boldSystemFontOfSize:14.0];
	statusLabel2.textAlignment = UITextAlignmentCenter;
	
	[self.view addSubview:testResultsBar];
	[self.view addSubview:passedTestsLabel];
	[self.view addSubview:failedTestsLabel];
	[self.view addSubview:statusLabel1];
	[self.view addSubview:statusLabel2];
	
	self.view.backgroundColor = [UIColor clearColor];
	
	testResultsBar.value = 0.0;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[testResultsBar release];
	[passedTestsLabel release];
	[failedTestsLabel release];
	[statusLabel1 release];
	[statusLabel2 release];
    [super dealloc];
}

#pragma mark -

- (void)updateDisplay {
	NSInteger totalPassingTests = 0;
	NSInteger totalFailingTests = 0;
	NSInteger totalTestCasesExecuted = 0;
	NSInteger totalTestCases = 0;
	NSInteger totalTests = 0;

	TestLocationModel *testLocationModel = [TestLocationModel sharedInstance];

	for (TestCaseRun *iTestCaseRun in testLocationModel.testCaseRuns) {
		if ([iTestCaseRun isCompleted]) {
			totalPassingTests += iTestCaseRun.passingTests;
			totalFailingTests += iTestCaseRun.failingTests;
			totalTestCasesExecuted++;
		}
		
		totalTests += [iTestCaseRun totalTests];
		totalTestCases++;
	}

	if (totalTests > 0) {
		statusLabel1.text = [NSString stringWithFormat:@"Tests %0.0f%% Completed", (double)(totalPassingTests + totalFailingTests) / (double)totalTests * 100.0];
		testResultsBar.value = ((double)totalPassingTests / (double)totalTests);
	} else {
		statusLabel1.text = [NSString stringWithFormat:@"No Tests", totalTests];
		statusLabel2.text = @"Check the Wiki to learn how to create a test.";
		testResultsBar.value = 1.0;
	}

	// messages
	if (totalPassingTests == totalTests) {
		statusLabel2.text = [self randomSuccessMessage];
	} else if (totalFailingTests < 4) {
		statusLabel2.text = [self randomSomeFailureMessage];
	} else {
		statusLabel2.text = [self randomManyFailureMessage];
	}		
	
	passedTestsLabel.text = [NSString stringWithFormat:@"%i", totalPassingTests];
	failedTestsLabel.text = [NSString stringWithFormat:@"%i", totalFailingTests];
}

- (NSString *)randomSuccessMessage {
	// all possible messages
	static NSArray *messages = nil;
	
	if (!messages) {
		messages = [[NSArray alloc] initWithObjects:@"You da man!", @"Whoot, there it is!", @"Perfect!", @"Swish!", @"Boom goes the dynamite!", @"Party time!", @"Did you expect anything less?", @"You deserve a raise!", @"Clearly, this app is bug-free.", @"Go on, brag.", @"YES!", @"Smile.", @"Good thing you studied..", nil];
	}

	return [messages objectAtIndex:arc4random()%[messages count]];
}

- (NSString *)randomSomeFailureMessage {
	// all possible messages
	static NSArray *messages = nil;
	
	if (!messages) {
		messages = [[NSArray alloc] initWithObjects:@"Aww, so close!", @"Well, that's not so bad.", @"Code a little better next time, OK?", @"So close.", @"Sorry, man. Just missed it.", @"Aww, shucks!", @"Next time, I want it perfect.", @"Fix it quick before someone sees!", @"Next time I know they'll pass.", @"One more time.", @"Not my bug.", @"Check it in, no one'll notice.", nil]; 
	}
	
	return [messages objectAtIndex:arc4random()%[messages count]];
}

- (NSString *)randomManyFailureMessage {
	// all possible messages
	static NSArray *messages = nil;
	
	if (!messages) {
		messages = [[NSArray alloc] initWithObjects:@"Dude, that's awful.", @"Ever write unit tests before?", @"How much do they pay you?", @"They must pay you by the bug.", @"That's not even close.", @"We could be here all day.", @"Embarrassing.", @"That's the best you could do?", @"Seriously?!", @"This may be a while.", nil];
	}
	
	return [messages objectAtIndex:arc4random()%[messages count]];
}

@end
